onShow () {
  this.login()
},
// 开始login
login () {
  let that = this
  wx.login({
    success (res) {
      if (res.code) {
        // 登录成功，获取用户信息
        that.getUserInfo(res.code)
      } else {
        // 否则弹窗显示，showToast需要封装
        wx.showToast({
          title: '获取code失败',
          icon: 'none'
        })
      }
    },
    fail () {
      wx.showToast({
        title: '微信登录失败',
        icon: 'none'
      })
    }
  })
},
// 获取用户信息
getUserInfo (code) {
  let that = this
  wx.getUserInfo({
    // 获取成功，全局存储用户信息，开发者服务器登录
    success (res) {
      store.commit('setWXUserInfo', res.userInfo)
      that.opOauthAccountXcx(code)
    },
    // 获取失败，弹窗提示一键登录
    fail () {
      wx.hideLoading()
      // 获取用户信息失败，清除全局存储的登录状态，弹窗提示一键登录
      // 如果使用token管理登录态的，清除存储全局的token
      // 如果使用cookie管理登录态的，可以清除全局登录状态管理的变量
      store.commit('setToken', '')
      wx.showToast({
        title: '你还未登录',
        icon: 'none'
      })
    }
  })
},
getUserInfo (e) {
  this.loginWechat(e)
},
// 登录微信,获取code
loginWechat (e) {
  let that = this
  wx.login({
    success: function (res) {
      if (res.code) {
        console.log(res.code)
        that.opOauthAccountXcx(res.code, e)
      } else {
        console.log('登录失败！' + res.errMsg)
      }
    }
  })
},
// 判断后台数据库是否有openid
opOauthAccountXcx (code, e) {
  let that = this
  var param = {}
  param['code'] = code
  param['type'] = 2
  const opOauthAccountXcx = api.opOauthAccountXcx(param)
  opOauthAccountXcx.then(d => {
    if (d.code === 200) {
      store.commit('setUserInfo', d.data)
      that.$router.push({ path: '/pages/home/main', isTab: true })
    } else if (d.code === 201) {
      that.opgetuserinfo(e.mp.detail.userInfo, d.data.openid, d.data.unionid)
      that.$router.push({ path: '/pages/accounts/bindingcellphone/main', query: { openid: d.data.openid, unionid: d.data.unionid } })
    }
  })
  .catch((e) => {
    console.log(e)
  })
},
// 后台存入userInfo和 openid
opgetuserinfo (userInfo, openid, unionid) {
  userInfo['openid'] = openid
  userInfo['unionid'] = unionid
  var param = userInfo
  console.log(userInfo)
  const opgetuserinfo = api.opgetuserinfo(param)
  opgetuserinfo.then(d => {
    if (d.code === 200) {
      console.log('保存成功')
    } else if (d.code === 4313) {
      console.log('保存失败')
    }
  })
  .catch((e) => {
    console.log(e)
  })
},
